//
//  NSString+TSFString.m
//  Tool
//
//  Created by 积分宝 on 16/8/18.
//  Copyright © 2016年 积分宝. All rights reserved.
//

#import "NSString+TSFString.h"

@implementation NSString (TSFString)
//判断一个字符串是否为空，如果为空赋一个初值
-(NSString *)checkStringIsNullWithNullString:(NSString *)string{
    NSString *oldString=self;
    if (oldString==nil || [oldString isEqualToString:@""] || [oldString isEqualToString:@"<null>"] || [oldString isEqualToString:@"(null)"]||[oldString isEqualToString:@"nul"]||[oldString isEqualToString:@"null"]||oldString==NULL) {
        oldString=string;
    }
    return oldString;
}
- (CGSize)boundingRectWithTextMAXWith:(float)MAXWith AndHeight:(float)MAXHeight AndFont:(CGFloat)textFont
{
    
    if (MAXHeight==0) {
        MAXHeight =CGFLOAT_MAX;
    }
    CGSize size = CGSizeMake(MAXWith,MAXHeight);
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:10]};
    
    CGSize retSize = [self boundingRectWithSize:size
                                          options:\
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                       attributes:attribute
                                          context:nil].size;
    
    return retSize;
}

-(CGSize)alertINtegralLWith:(CGFloat)stringFont{
    UIFont *  font = [UIFont systemFontOfSize:10];
    // 根据字体得到NSString的尺寸
    CGSize size = [self sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
    return size;
}

-(NSMutableAttributedString *)alterTypefaceColor:(NSString *)text AndColor:(UIColor *)color{
    
      NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:self];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:color
     
                          range:[self rangeOfString:text]];
    return AttributedStr;
    
}

-(NSMutableAttributedString *)alterTypefaceFontSize:(NSString *)text AndColor:(CGFloat)font{
     NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:self];
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:font]
     
                          range:[self rangeOfString:text]];
    
    return AttributedStr;
}
/*-(NSMutableAttributedString *)alterTypefaceFontSizeAndColor:(NSString *)text{
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:text];
    
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor colorWithHexString:@"#e85352"]
     
                          range:NSMakeRange(0,text.length)];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:14.0]
     
                          range:NSMakeRange(0,text.length)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor colorWithHexString:@"#505050"]
     
                          range:[text rangeOfString:@"退款金额:"]];
    
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:10.0]
     
                          range:[text rangeOfString:@"退款金额:¥"]];
    
    
    
    
    //处理小数点后两位
    int myIndex = 0;
    
    NSMutableArray *arr = [NSMutableArray array];
    for (int i=1; i<=text.length; i++) {
        [arr addObject:[text substringWithRange:NSMakeRange(i-1, 1)]];
        if ([arr[i-1] isEqualToString:@"."]) {
            myIndex = i;
        }
    }
    
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:10.0]
     
                          range:NSMakeRange(myIndex, 2)];
    
    
    return AttributedStr;
    
    
}*/


-(NSString *)transformToStarStringWithString:(NSString *)normalString {
    
    
    normalString = [normalString checkStringIsNullWithNullString:@"**"];
    
    int leng = (int)[normalString length];
    
    NSRange range = NSMakeRange(1, leng - 1);
    
    NSMutableString *starString = [[NSMutableString alloc] init];
    
    for (int i = 0; i < leng - 1; ++i) {
        [starString appendString:@"*"];
    }
    NSString *transformStarString = [normalString stringByReplacingCharactersInRange:range withString:starString];
    
    return transformStarString;
    
}


- (BOOL)isPureInt{
    NSScanner* scan = [NSScanner scannerWithString:self];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

/**
 *@brief 银行卡输入，textField4位加空格，16个数字后还能添加14位数字
 */
- (NSString *)addSpacingToLabelWithString
{
    
    NSString *newString;
    //检测是否为纯数字
    if ([self isPureInt]) {
        //添加空格，每4位之后，4组之后不加空格，格式为xxxx xxxx xxxx xxxx xxxxxxxxxxxxxx
        if (self.length % 5 == 4 && self.length < 22) {
            newString = [NSString stringWithFormat:@"%@ ", self];
        }
    }
    return newString;
}

#pragma mark - 手机号码正则验证
-(BOOL)validatePhone {
    NSString *phoneRegex = @"^1[3|4|5|6|7|8|9][0-9]\\d{8}$";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [emailTest evaluateWithObject:self];
}
-(BOOL)isEmail{
    
    
    NSString *emailRegex = @"^([a-zA-Z0-9]+[_|-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|-|.]?)*[a-zA-Z0-9]+(.(com|cn|net|co))+$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    
    return [emailTest evaluateWithObject:self];
}
@end
